package com.sg.zhui.visitor.utils;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by Anthony on 2016/5/23.
 * Class Note:
 * SharedPreference Helper class
 */
public class PreferencesHelper {
    private static final String SETTING = "setting";
    private final SharedPreferences mPref;
    public static final String SP_VERSION = "VERSION";
    public static final String KEY_VERSION_INT = "version";
    public static final String USER_INFO = "userInfo";
    private Context context;
    private static final String KEY_VERSON_TYPE = "type";

    private static final String USERID = "userId";

    /**
     *得到userId
     * @return
     */
    public String getUserId() {
        return getString(USERID, USERID, "");
    }

    /**
     *保存userId
     * @param userName
     */
    public void saveUserId(String userName) {
        saveString(USERID, USERID, userName);
    }

    /**
     * 获取版本号
     *
     * @return
     */
    public int getSaveVersion() {
        return context.getSharedPreferences(SP_VERSION, 0).getInt(
                KEY_VERSION_INT, 0);
    }

    /**
     * 保存当前版本号
     */
    public void saveCurrentVersions(int version) {
        context.getSharedPreferences(SP_VERSION, 0).edit()
                .putInt(KEY_VERSION_INT, version).commit();
    }

    /**
     * 保存更新版本号
     *
     * @param type
     */
    public void saveUpgradeType(int type) {
        context.getSharedPreferences(SP_VERSION, 0).edit()
                .putInt(KEY_VERSON_TYPE, type).commit();
    }

    public int getUpgradeType() {
        return context.getSharedPreferences(SP_VERSION, 0)
                .getInt(KEY_VERSON_TYPE, 0);
    }

    /**
     * 保存文本数据
     * @param spName 文件名
     * @param key    键
     * @param value  值
     */
    public void saveString(String spName, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        boolean commit = sp.edit().putString(key, value).commit();
//		LogOut.e("saveString:", commit+",key:"+key);
    }

    /**
     * 保存int数据
     *
     * @param spName 文件名
     * @param key    键
     * @param value  值
     */
    public void saveInt(String spName, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putInt(key, value).apply();
    }

    /**
     * 获取保存的Int数据
     *
     * @param spName
     * @param key
     * @param defValue
     * @return
     */
    public int getInt(String spName, String key, int defValue) {
        return context.getSharedPreferences(spName, 0).getInt(key, defValue);
    }

    public void saveBoolean(String spName, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences(spName, 0);
        sp.edit().putBoolean(key, value).apply();
    }

    public boolean getBoolean(String spName, String key, boolean defValue) {
        return context.getSharedPreferences(spName, 0).getBoolean(key, defValue);
    }

    /**
     * 删除指定的key
     *
     * @param spName
     * @param key
     */
    public void remove(String spName, String key) {
        context.getSharedPreferences(spName, 0).edit().remove(key).apply();
    }

    /**
     * 获取指定路径文本数据
     *
     * @param spName
     * @param key      键
     * @param defValue 值
     * @return
     */
    public String getString(String spName, String key, String defValue) {
        String string = context.getSharedPreferences(spName, Context.MODE_PRIVATE).getString(key, defValue);
        return string;
    }


    public PreferencesHelper(Context context) {
        this.context = context;
        mPref = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
    }

    public void clear() {
        mPref.edit().clear().apply();
        remove(USER_INFO, USER_INFO);
    }


}
